package com.tencent.qf.common.enums;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author doublezhu
 * @Date: 2021/12/15 10:46
 */
@AllArgsConstructor
@Getter
public enum UserLevelEnum {

    FAN(10, "FAN", "粉丝", 100),
    VIP(20, "VIP", "VIP用户", 200),
    TRAINEE(25, "TRAINEE", "见习会员", 300),
    MEMBER(30, "MEMBER", "初级会员", 400),
    SENIOR(40, "SENIOR", "高级会员", 500),
    SUPER(50, "SUPER", "超级会员", 600);

    /**
     * fan、vip用户
     */
    public static final List<String> FAN_VIP_LEVEL = Arrays.asList(FAN.value, VIP.value);

    public static final List<String> FAN_VIP_TRAINEE_LEVEL = Arrays.asList(FAN.value, VIP.value, TRAINEE.value);

    // vip以上等级
    public static final List<String> VIP_UPPER_LEVEL = Arrays.asList(VIP.value, TRAINEE.value,MEMBER.value, SENIOR.value, SUPER.value);

    public static final List<String> GROUP_LIST = Arrays.asList(TRAINEE.value, MEMBER.value, SENIOR.value, SUPER.value);

    /**
     * vip合并至粉丝（兼容处理）
     */
    public static final List<String> MARGE_VIP_LIST = Arrays.asList(FAN.value, VIP.value);


    public static final List<String> ALL_GROUP_LIST = Arrays.asList(FAN.value, VIP.value, TRAINEE.value, MEMBER.value, SENIOR.value, SUPER.value);

    public static final List<String> ALL_GROUP_LIST_DESC = Arrays.asList(FAN.desc, VIP.desc, TRAINEE.desc, MEMBER.desc, SENIOR.desc, SUPER.desc);

    /**
     * 见习到高级
     */
    public static final List<String> TRAINEE2SENIOR = Arrays.asList(TRAINEE.value, MEMBER.value, SENIOR.value);


    /**
     * 正常升级链路中的下一级
     */
    public static final Map<String, UserLevelEnum> NEXT_LEVEL_MAP
            = ImmutableMap.of(TRAINEE.value, MEMBER, MEMBER.value, SENIOR, SENIOR.getValue(), SUPER);

    /**
     * VIP往上等级
     */
    public static final List<UserLevelEnum> VIP_UP_LIST = Lists.newArrayList(TRAINEE, MEMBER, SENIOR, SUPER);

    /**
     * 见习会员往上可升等级
     */
    public static final List<UserLevelEnum> TRAINEE_UP_LIST = Lists.newArrayList(MEMBER, SENIOR, SUPER);

    /**
     * 初级会员往上可升等级
     */
    public static final List<UserLevelEnum> MEMBER_UP_LIST = Lists.newArrayList(SENIOR, SUPER);

    /**
     * 高级会员往上可升等级
     */
    public static final List<UserLevelEnum> SENIOR_UP_LIST = Lists.newArrayList(SUPER);

    private final int code;
    private final String value;
    private final String desc;
    /**
     * 等级层次。作用：查询等级列表的时候，中台按seq从小到大顺序返回
     */
    private final int sequence;

    /**
     * 判断nextLevel是否为curLevel的下一级
     *
     * @param curLevel
     * @param nextLevel
     * @return
     */
    public static boolean judgeNextLevel(UserLevelEnum curLevel, UserLevelEnum nextLevel) {
        if (curLevel == TRAINEE && nextLevel == MEMBER) {
            return true;
        }
        if (curLevel == MEMBER && nextLevel == SENIOR) {
            return true;
        }
        if (curLevel == SENIOR && nextLevel == SUPER) {
            return true;
        }
        return false;
    }

    public static boolean judgeNextLevel(String curLevel, String nextLevel) {
        if (Objects.equals(TRAINEE.value, curLevel) && Objects.equals(MEMBER.value, nextLevel)) {
            return true;
        }
        if (Objects.equals(MEMBER.value, curLevel) && Objects.equals(SENIOR.value, nextLevel)) {
            return true;
        }
        if (Objects.equals(SENIOR.value, curLevel) && Objects.equals(SUPER.value, nextLevel)) {
            return true;
        }
        return false;
    }

    public static UserLevelEnum getByValue(String value) {
        for (UserLevelEnum userLevelEnum : UserLevelEnum.values()) {
            if (userLevelEnum.getValue().equals(value)) {
                return userLevelEnum;
            }
        }
        return null;
    }

    /**
     * 获取用户可以升级的下一等级
     *
     * @param userLevelEnum
     * @return
     */
    public static List<UserLevelEnum> getLevelUpNextLevel(UserLevelEnum userLevelEnum) {
        if (userLevelEnum == null) {
            return null;
        }

        switch (userLevelEnum) {
            case FAN:
//                ArrayList<UserLevelEnum> list = new ArrayList<>();
//                list.add(FAN);
//                list.add(TRAINEE);
//                return list;
                return Collections.singletonList(TRAINEE);
//            case VIP:
//                return Collections.singletonList(TRAINEE);
            case TRAINEE:
                return TRAINEE_UP_LIST;
            case MEMBER:
                return MEMBER_UP_LIST;
            case SENIOR:
                return SENIOR_UP_LIST;
            case SUPER:
                return null;
        }
        return null;
    }

    /**
     * 判断是否为团长
     *
     * @param levelId
     * @return
     */
    public static boolean isGroup(String levelId) {
        return GROUP_LIST.contains(levelId);
    }


    public static String getNextLevelStr(String curLevelId) {
        UserLevelEnum curLevelEnum = getByValue(curLevelId);
        UserLevelEnum nextLevel = getNextLevel(curLevelEnum);
        if (nextLevel == null) {
            return null;
        }
        return nextLevel.getValue();
    }

    /**
     * 获取团长路线（见习会员及以上）用户的下一等级（不可跨级）
     *
     * @param userLevelEnum
     * @return
     */
    public static UserLevelEnum getNextLevel(UserLevelEnum userLevelEnum) {
        if (userLevelEnum == null) {
            return null;
        }
        switch (userLevelEnum) {
            case TRAINEE:
                return MEMBER;
            case MEMBER:
                return SENIOR;
            case SENIOR:
                return SUPER;
            case SUPER:
                return null;
        }
        return null;
    }

    /**
     * 获取团长路线（见习会员及以上）用户的上一等级（不可跨级）
     *
     * @param userLevelEnum
     * @return
     */
    public static UserLevelEnum getHeadLevel(UserLevelEnum userLevelEnum) {
        if (userLevelEnum == null) {
            return null;
        }
        switch (userLevelEnum) {
            case MEMBER:
                return TRAINEE;
            case SENIOR:
                return MEMBER;
            case SUPER:
                return SENIOR;
            case TRAINEE:
                return null;
        }
        return null;
    }

    public static List<String> getAllValue() {
        List<String> list = new ArrayList<>();
        for (UserLevelEnum userLevelEnum : UserLevelEnum.values()) {
            list.add(userLevelEnum.getValue());
        }
        return list;
    }

    /**
     * 后端兜底逻辑 - 判断上级的等级，如果上级是粉丝、vip、见习会员则不能绑定
     *
     * @param parentLevelId
     * @return
     */
    public static boolean checkInviteValue(String parentLevelId) {
        if (FAN_VIP_TRAINEE_LEVEL.contains(parentLevelId)) {
            return true;
        }
        return false;
    }
}
